Given an integer, write a function to determine if it is a power of two.

Example 1:

Input: 1

Output: true

Example 2:

Input: 16

Output: true

Example 3:

Input: 218

Output: false

static int x=[](){

std::ios::sync\_with\_stdio\(false\);

cin.tie\(NULL\);

return 0;

}();

class Solution {

public:

bool isPowerOfTwo\(int n\) {

    int result=1;

    int flag=false;

    if \(\(n==2\)\|\|\(n==1\)\) return true;

    int temp=n;

    if \(n%2==1\) return false;

    for \(int i=1;i<temp;i++\){

        if \(n%2==1\) return false;

        n/=2;

        if\(n==2\) {

            flag=true;

            break;

        }

        //cout<<n<<endl;

    }



    return flag;

}

};

关掉scanf这个函数真的很有用。。。3%到100%

results matching ""

    No results matching ""